package com.javabase.y2022.m05.day0517.v5;

import java.util.HashMap;

/**
 * @descriptions: Solution
 * @author: Tom
 * @date: 2022/5/17 上午 10:36
 * @version: 1.0
 */
public class Solution {
    public static void main(String[] args) {
        String[] words = new String[]{"hello","hello"};
        String order = "abcdefghijklmnopqrstuvwxyz";
        new Solution().isAlienSorted(words, order);
    }
    public boolean isAlienSorted(String[] words, String order) {
        String[] convertedWords = new String[words.length];
        HashMap<Character, String> map = createOrderSet(order);
        for (int i = 0; i < words.length; i++) {
            StringBuilder stringBuilder = new StringBuilder();
            for(int j = 0; j < words[i].length(); j++){
                stringBuilder.append(map.get(words[i].charAt(j)));
            }
            convertedWords[i] = stringBuilder.toString();
            if(i >= 1){
                boolean ans = convertedWords[i].compareTo(convertedWords[i-1]) >= 0;
                if(!ans){
                    return false;
                }
            }
        }
        return true;
    }
    public  HashMap<Character, String> createOrderSet(String order){
        HashMap<Character, String> map = new HashMap<>();
        for (int i = 0; i < order.length(); i++) {
            if(i <= 9){
                map.put(order.charAt(i),"0"+i);
            } else {
                map.put(order.charAt(i),""+i);
            }
        }
        return map;
    }
}
